# -*- coding: utf-8 -*-
"""
数据库连接配置
"""
import mysql.connector
from mysql.connector import connect, Error
import pymysql

# 修改数据库连接配置
DATABASE_CONFIG = {
    'host': 'localhost',
    'port': 3306,
    'user': 'root',
    'password': '123456',
    'database': 'fastapi',
    'charset': 'utf8mb4',
    'collation': 'utf8mb4_0900_ai_ci',
    'autocommit': True
}

def create_db_connection():
    """创建数据库连接"""
    try:
        connection = connect(**DATABASE_CONFIG)
        return connection
    except Error as e:
        print(f"数据库连接失败: {e}")
        return None

def get_db_connection():
    """获取数据库连接（别名）"""
    return create_db_connection()

# 在数据库连接字符串中添加校对规则
DATABASE_URL = "mysql+pymysql://username:password@localhost/database_name?charset=utf8mb4&collation=utf8mb4_unicode_ci"

def get_database_connection():
    return pymysql.connect(
        host='localhost',
        port=3306,
        user='your_username',
        password='your_password',
        database='your_database',
        charset='utf8mb4',
        collation='utf8mb4_unicode_ci',  # 明确指定校对规则
        autocommit=True
    )